package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.ProjectMember;
import com.kaishengit.util.db.DBHelp;
import com.kaishengit.util.db.RowMapper;

public class ProjectMemberDao {
	
	private DBHelp<ProjectMember> db = new DBHelp<ProjectMember>();
	
	public void save(ProjectMember pm){
		String sql="insert into t_project_member (id,userId,projectId,role) values(?,?,?,?)";
		db.executeSQL(sql, pm.getId(),pm.getProjectId(),pm.getProjectId(),pm.getRole());
	}
	
/*	public	List<ProjectMember> findProjectByUserId(User user){
		String sql = "SELECT tp.id =? , NAME = ?, description = ?  FROM t_project AS tp LEFT JOIN t_project_member AS tpm ON tp.id = tpm.projectId WHERE userId = ?";
		
		return db.executeQueryToList(sql, new ProjectMemberRowMapper(){

			public ProjectMember mapRow(ResultSet rs) throws SQLException {
				// TODO Auto-generated method stub
				return null;
			}
			
		}, user.getId());
		
	}*/
	
	public	List<ProjectMember> findMembersByProjectId(ProjectMember pm){
		String sql="select id=?,userid=?,role=? from t_project_member where projectId=?";
		return db.executeQueryToList(sql, new ProjectMemberRowMapper(), "projectId");
	}
	
	private class ProjectMemberRowMapper implements RowMapper<ProjectMember>{
		public ProjectMember mapRow(ResultSet rs) throws SQLException {
			ProjectMember pm = new ProjectMember();
			
			pm.setId(rs.getInt("id"));
			pm.setUserId(rs.getInt("userId"));
			pm.setProjectId(rs.getInt("projectId"));
			pm.setRole(rs.getString("role"));
			
			return null;
		}
	}	
}
